package test.xlss;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.aat.dxfy.http.HttpPoolUtil;

public class ReadXlss {

	public static void main(String[] args) throws Exception {
		InputStream is = new FileInputStream("/Users/Able/Desktop/juzi3000.xlsx");
		Workbook wb = WorkbookFactory.create(is);
		Sheet sheet = wb.getSheetAt(0);
		System.out.println("name:" + sheet.getSheetName());
		// 总行数
		int trLength = sheet.getLastRowNum();
		Row row = sheet.getRow(0); // 第一行
		// 总列数
		int tdLength = row.getLastCellNum();
		System.out.println("row:" + trLength + ",col:" + tdLength);
		Cell cell = row.getCell(0);// 第一行的第一个表格
		System.out.println("val:" + cell.getStringCellValue());
		CellStyle cs = cell.getCellStyle();
		System.out.println("style:" + cs.getAlignment());

		// 循环读取,RFCL,row first col last
		for (int i = 0; i < trLength; i++) {
			Row rowTmp = sheet.getRow(i);
			ArrayList<String> rowVals=new ArrayList<String>();
			for (int j = 0; j < tdLength; j++) {
				Cell c=rowTmp.getCell(j);
				int cellType =c.getCellType();
				if(cellType==Cell.CELL_TYPE_STRING){
					rowVals.add(c.getStringCellValue());
				}
				if(cellType==Cell.CELL_TYPE_NUMERIC){
					rowVals.add(c.getNumericCellValue()+"");
				}
				if(cellType==Cell.CELL_TYPE_FORMULA){
					rowVals.add(c.getCellFormula());
				}
				if(cellType==Cell.CELL_TYPE_ERROR){
					rowVals.add(c.getErrorCellValue()+"");
				}
				if(cellType==Cell.CELL_TYPE_BOOLEAN){
					rowVals.add(c.getBooleanCellValue()+"");
				}
				if(cellType==Cell.CELL_TYPE_BLANK){
					rowVals.add("");
				}
				
			}
			Map<String, String> params = new HashMap<>();
			params.put("book_name", rowVals.get(0));
			params.put("book_author", rowVals.get(2));
			params.put("book_host", rowVals.get(1));
			addBook(params);
			System.out.println(params);
		}
		is.close();
		
	}
	
	public static void addBook(Map<String, String> params) {
		String uri = "http://book.qiaolea.com/api/v1/book/add";
		HttpPoolUtil.post(uri, params);
	}

}
